package com.resource.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.json.JSONUtil;
import com.resource.response.OfficeResponse;
import com.resource.service.OfficeTokenService;
import com.resource.service.TicketTokenService;
import com.resource.utils.HttpUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.resource.dto.ProjectTaskParamDto;
import com.resource.request.TicketBaseRequest;
import com.resource.response.TicketCallbackResponse;
import com.resource.service.TicketService;
import com.resource.utils.ServiceException;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;

/**
 * 工单接口
 * 
 * @Author: xianggaoxiong
 * @Date: 2021-01-07 11:41
 **/
@RestController
@RequestMapping(value = "/api/openCloud/ticket_info")
public class TicketController {
	@Resource
	private TicketService ticketService;
	@Resource
	private TicketTokenService ticketTokenService;

	@GetMapping("/token")
	public String getToken(){
		return ticketTokenService.getToken();
	}

	/**
	 * 智齿工单同步泛微
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/call_back/sync")
	public TicketCallbackResponse syncOffice(@RequestBody TicketBaseRequest request) {
		try {
			ticketService.syncOffice(request);
		} catch (ServiceException e) {
			return TicketCallbackResponse.builder().retCode("999999").retMsg(e.getMessage()).build();
		}
		return TicketCallbackResponse.builder().retCode("000000").retMsg("数据接收成功").build();
	}

	/**
	 * 泛微任务同步智齿
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/call_back/sync/ticket")
	public Boolean syncTicket(HttpServletRequest request) {
		String taskProject = request.getParameter("request[task_project]");
		String taskId = request.getParameter("request[task_id]");
		String userId = request.getParameter("user_info[user_id]");
		String userName = request.getParameter("user_info[user_name]");
		String fpi = request.getParameter("request[fpi]");
		ProjectTaskParamDto build = ProjectTaskParamDto.builder().task_id(Long.valueOf(taskId)).fpi(fpi)
				.task_project(Long.valueOf(taskProject)).user_id(userId).user_name(userName).build();
		return ticketService.syncTicket(build);
	}


	@Resource
	private OfficeTokenService officeTokenService;
	@Resource
	private RestTemplate restTemplate;
	@RequestMapping("/upload/file")
	public boolean upload() throws Exception{
		String url="http://oa-mc.test027.com/eoffice10/server/public/api/attachment/upload";
		//设置请求头
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("multipart/form-data");
		headers.setContentType(type);
		String token = officeTokenService.getToken("admin");
		headers.set("Authorization", "Bearer " + token);

		URL uri = new URL("https://file.sijibao.com/wyyt-doc/2019/03/04/0371军涛土石方.PDF_8974635398650009654.pdf");
		FileUtils.copyURLToFile(uri, new File("/tmp"+ File.separator+"1.pdf"));
		MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
		form.add("Filedata",  new File("/tmp"+ File.separator+"1.pdf"));
		form.add("chunks",1);
		form.add("abbreviation","other");
		form.add("name","1.pdf");
		form.add("chunk",0);

		//用HttpEntity封装整个请求报文
		HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
		ResponseEntity<OfficeResponse> responseEntity = restTemplate.exchange(url, HttpMethod.POST, files,
				OfficeResponse.class);
		System.out.println(JSONUtil.toJsonStr(responseEntity));
		return true;
	}



}
